﻿using Minecraft.BLL.mysql;
using Minecraft.Config;
using Minecraft.Model;
using Minecraft.Model.Backpack;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Minecraft.ServerHall.Cmd
{
	public class BackpackGoodsDelete : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Backpack_BackpackGoodsDelete;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<BackpackGoodsDeleteReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：未登录）" });
				return;
			}
			//物品来源判断
			if (!MinecraftConfig.enumGoodsSourceBelongsToTypeList.Any(m => m == req.enumGoodsBelongsToType))
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：物品来源不正确，前端传入的物品来源为【{req.enumGoodsBelongsToType.ToString()}】）" });
				return;
			}
			if (req.GoodsPosition < 0)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误2（原因：物品位置小于0）" });
				return;
			}
			if (req.guid.IsNullOrWhiteSpace())
			{
				req.guid = "";
			}
			var baseResp = req.enumGoodsBelongsToType.GetBackpackGoodsBelongsToTypeBaseType(req.GoodsPosition, session, "BackpackGoodsDelete", req.guid);
			if (baseResp.RespLevel != RespLevelEnum.Success)
			{
				session.Send(defCommand, req, baseResp);
				return;
			}

			//if (req.goodsList == null || !req.goodsList.Any())
			//{
			//	session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误3（原因：请求物品列表为空）" });
			//	return;
			//}

			//if (req.goodsId.IsNullOrWhiteSpace())
			//{
			//	session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误2（原因：goodsId 参数为空）" });
			//	return;
			//}
			if (req.num <= 0)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误2（原因：num <= 0）" });
				return;
			}
			//进一步检验goodslist的合法性
			Func<GoodsModel, bool> predicate = m =>
				m.enumGoodsBelongsToType_Convert == req.enumGoodsBelongsToType && m.GoodsPosition == req.GoodsPosition && m.Num > 0 && m.guid == req.guid;

			var goodsInfo = session.minecraftSessionInfo.goodsList.FirstOrDefault(predicate);
			if (goodsInfo == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误4（原因：当前位置物品为空，无法删除）" });
				return;
			}
			//var exceptList = req.goodsList.Except(curGoodsIdList.Select(m => m.GoodsId)).ToList();
			//if (exceptList.Any())
			//{
			//	session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误5（原因：物品id集合并不全是在同一个格子中）" });
			//	return;
			//}

			if (req.num > goodsInfo.Num)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误4（原因：当前位置物品的物品个数小于请求num的参数）" });
				return;
			}

			//更新内存
			goodsInfo.Num -= req.num;
			

			////更新内存
			//session.minecraftSessionInfo.goodsList.RemoveAll(match => { return req.goodsList.Any(m => m == match.GoodsId); });

			//响应数据筛选
			var remainBackpackShortcutBarCellInfo = session.minecraftSessionInfo.goodsList
				.Where(predicate).GetBackpackGoodsCellInfo().FirstOrDefault();
			if (remainBackpackShortcutBarCellInfo == null)
			{
				remainBackpackShortcutBarCellInfo = new BackpackCellInfo
				{
					enumGoodsBelongsToType = req.enumGoodsBelongsToType,
					GoodsPosition = req.GoodsPosition,
					//GoodsItemId = curGoodsIdList.FirstOrDefault().GoodsItemId,
					GoodsItemId = goodsInfo.GoodsItemId,
					//GoodsList = new List<Model.Backpack.BackpackGoodsInfo>(),
					GoodsId = goodsInfo.guid,
					Num = goodsInfo.Num,
					WastageValue = goodsInfo.WastageValue,
					guid = goodsInfo.guid,
				};
			}

			session.minecraftSessionInfo.goodsList.RemoveAll(m => m.Num <= 0);

			var resp = new BackpackGoodsDeleteResp
			{
				remainBackpackShortcutBarCellInfo = remainBackpackShortcutBarCellInfo,
				enumGoodsBelongsToType = goodsInfo.enumGoodsBelongsToType_Convert,
				guid = goodsInfo.guid,
			};
			session.Send(defCommand, req, resp);
		}
	}
}
